<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" icon="el-icon-plus" @click="Add"
          >添加</el-button
        >
      </div>
      <el-table :data="tmList" border style="width: 100%">
        <el-table-column label="序号" width="180" type="index" align="center">
        </el-table-column>
        <el-table-column label="品牌名称" prop="tmName"> </el-table-column>
        <el-table-column label="品牌LOGO">
          <template slot-scope="{ row }">
            <el-image
              :src="row.logoUrl"
              style="width: 100px; height: 60px"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <div>
              <el-button
                type="warning"
                size="mini"
                icon="el-icon-edit"
                @click="Edit(row)"
                >修改</el-button
              >
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="del(row)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[3, 5, 7, 9]"
        :page-size="pageSize"
        layout="prev, pager, next, jumper, ->,sizes, total"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      :title="`${this.tmForm.id ? '修改' : '添加'}品牌`"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="tmForm">
        <el-form-item label="品牌名称:" label-width="100px">
          <el-input
            tyle="width: 80%"
            autocomplete="off"
            v-model="tmForm.tmName"
          ></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO:" :label-width="'100px'">
          <el-upload
            class="avatar-uploader"
            action="/api/admin/product/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip" slot="tip">
              只能上传jpg/png文件，且不超过2MB
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "Trademark",
  data() {
    return {
      page: 1, //当前页
      pageSize: 3, //每页多少
      dialogFormVisible: false,
      tmForm: {
        tmName: "", //品牌名称
        logoUrl: "", //品牌LOGO
      },
    };
  },
  mounted() {
    this.getTmList();
  },
  methods: {
    async getTmList() {
      try {
        await this.$store.dispatch("trademark/getTmList", {
          page: this.page,
          pageSize: this.pageSize,
        });

        this.$message.success("请求品牌列表成功");
      } catch (error) {
        this.$message.error("请求品牌列表失败");
      }
    },

    handleSizeChange(pageSize) {
      //拿着最新的条数发送请求
      this.pageSize = pageSize;
      this.getTmList(pageSize);
    },

    handleCurrentChange(page) {
      //拿着最新的页码发送请求
      this.page = page;
      this.getTmList(page);
    },

    Add() {
      this.dialogFormVisible = true;
      //每次进入都要清除tmName 和logoUrl
      this.tmForm.tmName = "";
      this.tmForm.logoUrl = "";
    },

    handleAvatarSuccess(res) {
      //  response 成功后的数据
      this.tmForm.logoUrl = res.data;
    },

    beforeAvatarUpload(file) {
      let typeArr = ["image/jpg", "image/png", "image/jpeg"];
      const isType = typeArr.indexOf(file.type) !== -1;

      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isType) {
        this.$message.error("必须上传jpg或png格式图片 ");
      }
      if (!isLt2M) {
        this.$message.error("图片超过2MB");
      }

      return isType && isLt2M;
    },

    Edit(row) {
      this.dialogFormVisible = true;
      this.tmForm = { ...row };
    },

    async confirm() {
      try {
        await this.$store.dispatch("trademark/getAddOrEdit", this.tmForm);

        this.dialogFormVisible = false;
        this.$message.success(`${this.tmForm.id ? "修改" : "添加"}品牌成功`);
        if (!this.tmForm.id) {
          this.page = 1;
        }
        this.getTmList();
      } catch (error) {
        this.$message.error(`${this.tmForm.id ? "修改" : "添加"}品牌失败`);
      }
    },

    async del(row) {
      const configResult = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "删除",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => {
        return err;
      });
      if (configResult !== "confirm") {
        return this.$message.info(" 已取消删除 ");
      }
      try {
        await this.$store.dispatch("trademark/getDeleteTrademark", row.id);
        this.getTmList();
        this.$message.success("删除成功");
      } catch (error) {
        this.$message.error("删除失败");
      }
    },
  },
  computed: {
    ...mapState({
      tmList: (state) => state.trademark.tmList,
      total: (state) => state.trademark.total,
    }),
  },
};
</script>

<style scoped>
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
